var express = require('express');
var router = express.Router();
const moment = require('moment')

//菜单管理
router.get('/admin_nav/list', function (req, res, next) {
    res.render('admin_tmpl/admin_nav/list.html', {});
});

//菜单修改
router.get('/admin_nav/edit', async (req, res, next) => {
    var id = req.query.id;
    var selectSQL = 'select * from t_admin_nav where delete_flag = 0 and id="' + id + '"'
    await common.prototype.operation(selectSQL).then(rest => {
        var adminNav = rest[0];
        res.render('admin_tmpl/admin_nav/edit.html', adminNav);
    }, (error) => {
        console.log(error)
        res.send(error)
    }).catch((err) => {
        console.log(err)
    })

});

//菜单添加
router.get('/admin_nav/add', async (req, res, next) => {
    var parentSQL = 'select * from t_admin_nav where delete_flag = 0 and level="' + 1 + '"'
    await common.prototype.operation(parentSQL).then(rest => {
        res.render('admin_tmpl/admin_nav/add.html', {parent: rest});
    }, (error) => {
        console.log(error)
        res.send(error)
    }).catch((err) => {
        console.log(err)
    })

});

/*
 * 首页菜单列表
 * @return
 *
 */
router.post('/adminNav', async (req, res, next) => {
    var selectSQL = 'select * from t_admin_nav where delete_flag = 0'
    await common.prototype.operation(selectSQL).then(rest => {
        var result = common.prototype.navGroup(rest)
        res.send(result)
    }, (error) => {
        console.log(error)
        res.send(error)
    }).catch((err) => {
        console.log(err)
    })
});

/*
 * 菜单列表
 * @return
 *
 */
router.post('/admin_nav/getList', async (req, res, next) => {
    var condition = '';
    var SQL = '';
    var conditionSQL = '';
    var allData = '';
    var begin = 0;
    var data = req.body;
    var pageSize = data.pageSize;
    var pageNumber = data.pageNumber;
    var likeTitle = req.body.search_like_title;
    if (pageSize) {
        begin = (pageNumber - 1) * pageSize;
        condition = condition + 'order by id desc limit ' + begin + ','+pageSize+''
    }
    if (likeTitle) {
        SQL = 'select * from t_admin_nav where delete_flag = 0 and title like "%' + likeTitle + '%"'
    } else {
        SQL = 'select * from t_admin_nav where delete_flag = 0'
    }
    await common.prototype.operation(SQL).then(async rest => {
        allData = rest;
        if (likeTitle) {
            conditionSQL = 'select * from t_admin_nav where delete_flag = 0 and title like  "%' + likeTitle + '%"' + condition + ''
        } else {
            conditionSQL = 'select * from t_admin_nav where delete_flag = 0 ' + condition + ''
        }
        await common.prototype.operation(conditionSQL).then(async result => {
            res.send({
                code: 0, msg: '操作成功',
                count: allData.length, data: result
            })
        })
    }, (error) => {
        console.log(error)
        res.send({
            code: 404, msg: '操作失败',
            data: error
        })
    }).catch((err) => {
        console.log(err)
    })
});

/*
 * 删除菜单
 * @return
 *
 */
router.post('/admin_nav/delete', async (req, res, next) => {
    var id = req.body.id;
    await common.prototype.operation('select * from t_admin_nav where parent=' + id + ' ').then(async rest => {
        if (rest.length > 0) {
            res.send({
                type: 'error', content: '请先删除子节点'
            })
        } else {
            await common.prototype.operation('delete from t_admin_nav where id=' + id + ' ').then(async result => {
                res.send({
                    type: 'success', content: '删除成功'
                })
            }, (error) => {
                console.log(error)
                res.send({
                    type: 'error', content: '删除失败'
                })
            })
        }
    }, (error) => {
        console.log(error)
        res.send({
            type: 'error', content: '删除失败'
        })
    }).catch((err) => {
        console.log(err)
    })
});


/*
 * 修改菜单
 * @param id
 * @param title
 * @param url
 * @param icon
 * @param orders
 * @param modify_date
 * @return
 *
 */
router.post('/admin_nav/update', async (req, res, next) => {
    var data = req.body;
    if (data.id) {
        var date = moment().format('YYYY-MM-DD HH:mm:ss');
        var SQL = 'update t_admin_nav set title="' + data.title + '", modify_date="' + date + '", url="' + data.url + '", icon="' + data.icon + '", orders="' + data.order + '" where id="' + data.id + '"'
        await common.prototype.operation(SQL).then(rest => {
            res.send({
                type: 'success', content: '添加成功'
            })
        }, (error) => {
            console.log(error)
            res.send({
                type: 'error', content: '添加失败'
            })
        }).catch((err) => {
            console.log(err)
        })
    } else {
        res.send({
            type: 'apiexception', content: '接口异常'
        })
    }
});

/*
 * 添加菜单
 *
 * @param username
 * @param password
 * @param roleType
 * @return
 *
 */
router.post('/admin_nav/addAdminNav', async (req, res, next) => {
    var data = req.body;
    var level;
    var date = moment().format('YYYY-MM-DD HH:mm:ss');
    if (data.parentname === '不关联菜单') {
        data.parentname = '';
        data.parent = '';
        level = 1
    } else {
        level = 2
    }
    // if (!data.order) {
    //     data.order = 0
    // }
    var SQL = 'INSERT INTO `t_admin_nav`(title,url,icon,orders,parent_name,level,sort,parent,create_date,modify_date,delete_flag) VALUES("' + data.title + '","' + data.url + '","' + data.icon + '","' + data.order + '","' + data.parentname + '","' + level + '","' + data.order + '","' + data.parent + '","' + date + '","' + date + '",0) '
    await common.prototype.operation(SQL).then(rest => {
        res.send({
            type: 'success', content: '添加成功'
        })
    }, (error) => {
        console.log(error)
        res.send({
            type: 'error', content: '添加失败'
        })
    }).catch((err) => {
        console.log(err)
    })
});


module.exports = router;